package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.SysDictItem;
import com.lu.statistics.mapper.SysDictItemMapper;
import com.lu.statistics.service.SysDictItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统数据字典详情-ServiceImpl
 */
@Service
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {
    @Autowired
    private SysDictItemMapper dictItemMapper;

    @Override
    public IPage<SysDictItem> queryPageDictItemList(SysDictItem dictItem, int current, int pageSize) {
        Page<SysDictItem> page = new Page(current, pageSize);
        QueryWrapper<SysDictItem> wrapper = new QueryWrapper<>();
        if (StringUtils.isNoneEmpty(dictItem.getItemCode())) {
            wrapper.like(true, "item_code", dictItem.getItemCode().trim());
        }
        if (StringUtils.isNoneEmpty(dictItem.getItemName())) {
            wrapper.like(true, "item_name", dictItem.getItemName().trim());
        }
        IPage<SysDictItem> dictIPage = dictItemMapper.selectPage(page, wrapper);
        return dictIPage;
    }

    @Override
    public List<SysDictItem> queryDictItem(String dictCode) {
        return dictItemMapper.findDictListByDictCode(dictCode);
    }

    @Override
    public SysDictItem queryDictItemInfo(int id) {
        return dictItemMapper.selectById(id);
    }

    @Override
    public void saveOrUpdateDictItem(SysDictItem dictItem) {
        if (dictItem.getId() != null) {
            dictItemMapper.updateById(dictItem);
        } else {
            dictItemMapper.insert(dictItem);
        }
    }

    @Override
    public void deleteDictItem(int id) {
        dictItemMapper.deleteById(id);
    }
}
